// src/logical/user/user.controller.ts
import { Controller, Post, Body, Headers, UseGuards } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';

import {
  ApiTags,
  ApiSecurity,
  ApiExtraModels,
  ApiBody,
  ApiOperation,
} from '@nestjs/swagger';
import { RpAccount } from '../entity/account.entity';
import { JwtService } from '@nestjs/jwt';
import { QueryModel } from '../../common/models/Query.model';
import { AccountQueryModel } from '../models/accountQuery.model';
import { Pagination } from '../../common/models/Pagination.model';
import { BaseListResultModel } from '../../common/models/BaseListResult.model';
import { RpAccountViewModel } from '../models/accountView.model';
import {
  ApiPaginatedResponse,
  ApiCommonResponse,
  ApiObjResponse,
} from 'src/decorator/api-response.decorator';
import { ApiQueryBodyRequest } from 'src/decorator/api-request.decorator';
import { BaseResultModel } from '../../common/models/BaseResult.model';
import { AccountIdModel } from '../models/accountId.model';
import { AuthService } from 'src/logical/auth/service/auth.service';
import { AccountService } from '../service/account.service';
@ApiSecurity('Authorization')
@ApiTags('用户账号管理')
@ApiExtraModels(AccountQueryModel, RpAccountViewModel)
@Controller('/api/account')
export class AccountController {
  constructor(
    private readonly authService: AuthService,
    private readonly accountService: AccountService,
    private readonly jwtService: JwtService,
  ) {}

  @UseGuards(AuthGuard('jwt'))
  @ApiCommonResponse()
  @ApiOperation({ summary: '保存' })
  @Post('save')
  async save(@Headers('authorization') token, @Body() body: RpAccount) {
    const tokenInfo = this.jwtService.decode(token);
    return await this.accountService.save(body, tokenInfo);
  }

  @UseGuards(AuthGuard('jwt'))
  @ApiPaginatedResponse(RpAccountViewModel)
  @ApiBody(ApiQueryBodyRequest('queryModel', AccountQueryModel))
  @ApiOperation({ summary: '查询列表' })
  @Post('queryForList')
  async queryForList(
    @Body() queryModel: QueryModel<AccountQueryModel, Pagination>,
  ): Promise<BaseListResultModel<Array<RpAccountViewModel>>> {
    return await this.accountService.queryForList(
      queryModel.query,
      queryModel.pagination,
    );
  }

  @UseGuards(AuthGuard('jwt'))
  @ApiObjResponse(RpAccountViewModel)
  @ApiOperation({ summary: '查询详细' })
  @Post('queryDetail')
  async queryDetail(
    @Body() body: AccountIdModel,
  ): Promise<BaseResultModel<RpAccountViewModel>> {
    const id = body.id;

    return await this.accountService.queryDetail(id);
  }
}
